package com.gdut.huayan.controller;

import com.gdut.huayan.entity.dto.PageQuery;
import com.gdut.huayan.entity.dto.Result;
import com.gdut.huayan.entity.po.Festival;
import com.gdut.huayan.service.FestivalService;
import com.gdut.huayan.service.FileService;
import com.gdut.huayan.validation.group.GroupAdd;
import com.gdut.huayan.validation.group.GroupUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;

@RestController
@RequestMapping("/festival")
public class FestivalController {

    @Autowired
    private FestivalService festivalService;

    @Autowired
    private FileService fileService;

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping ("/getAll")
    public Result getAll(@RequestBody PageQuery pageQuery){
        return Result.success(festivalService.getPage(pageQuery));
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/add")
    public Result addFestival(@RequestBody @Validated({Default.class, GroupAdd.class}) Festival festival,
                              String coverRequestId,
                              String backgroundRequestId){
        festival.setCoverFileId(fileService.getFileStatus(coverRequestId,true));
        festival.setBackgroundFileId(fileService.getFileStatus(backgroundRequestId,true));
        festivalService.save(festival);
        return Result.success();
    }

    //@GetMapping("/upload")
    public Result getUploadLink(){
        return Result.success(festivalService.getUploadLink());
    }

    @PreAuthorize("permitAll()")
    @GetMapping("/get")
    public Result get(){
//        System.out.println("这是小程序的获取节日列表");
        return Result.success(festivalService.get());
    }

    @PreAuthorize("permitAll()")
    @GetMapping("/getFestival")
    public Result get(Long festivalId){
//        System.out.println("这里获取节日列表");
        return Result.success(festivalService.get(festivalId));
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/update")
    public Result updateFestival(@RequestBody @Validated({Default.class, GroupUpdate.class}) Festival festival){
        festivalService.updateById(festival);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @DeleteMapping("/delete")
    public Result deleteFestival(Long festivalId){
        festivalService.removeById(festivalId);
        return Result.success();
    }

}
